//
//  LKMyTestRecordViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/10.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKMyTestRecordViewController.h"
#import "LKTestPageCell.h"
#import "LKTestListModel.h"
#import "LKTestpageNameViewController.h"
@interface LKMyTestRecordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSMutableArray *dataListArray;
@property(nonatomic,assign)NSInteger MaxResultCount;
@property(nonatomic,assign)NSInteger PageIndex;
@end

@implementation LKMyTestRecordViewController

-(LKBaseTableview *)tableView{
    if (!_tableView) {
        _tableView = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, 12 + NAVIGATION_BAR_HEIGHT, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 12)) style:UITableViewStyleGrouped registercellClasss:@[@"LKTestPageCell"]];
        [self.view addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
    }
    return _tableView;
}
/**
 头部下拉刷新
 */
-(void)headerReloadData{
    self.PageIndex = 1;
    [self getAllWaitingTestPage];
}

/**
 尾部上拉加载更多
 */
-(void)footerReloadData{
    self.PageIndex++;
    [self getAllWaitingTestPage];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 200;
    self.PageIndex = 1;
    self.MaxResultCount = 30;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self getAllWaitingTestPage];
    // Do any additional setup after loading the view.
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"考试记录";
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataListArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKTestPageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKTestPageCell"];
    cell.listModel = self.dataListArray[indexPath.section];
    [cell.bottomBtn setTitle:@"查看结果" forState:UIControlStateNormal];
    [cell.bottomBtn addTarget:self action:@selector(gogogoSee:) forControlEvents:UIControlEventTouchUpInside];
    cell.bottomBtn.tag = 10000 + indexPath.section;
    return cell;
}
-(void)gogogoSee:(UIButton *)btn{
    LKTestListModel *model = self.dataListArray[btn.tag - 10000];
    LKTestpageNameViewController *vc = [[LKTestpageNameViewController alloc] init];
    vc.paperId = model.Id;
    vc.paperName = model.examinationPaperName;
    [self.navigationController pushViewController:vc animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0000000000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 13;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = cellHeaderBgColor;
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LKTestListModel *model = self.dataListArray[indexPath.section];
    LKTestpageNameViewController *vc = [[LKTestpageNameViewController alloc] init];
    vc.paperId = model.Id;
    vc.paperName = model.examinationPaperName;
    [self.navigationController pushViewController:vc animated:YES];
}
-(NSMutableArray *)dataListArray{
    if (!_dataListArray) {
        _dataListArray = [[NSMutableArray alloc] init];
    }
    return _dataListArray;
}

/**
 获取所有的将要考试的列表
 */
-(void)getAllWaitingTestPage{
    NSString *pageIndexStr = [NSString stringWithFormat:@"%ld",self.PageIndex];
    NSString *maxRsult = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:pageIndexStr,@"PageIndex",maxRsult,@"MaxResultCount", nil];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:examnationListUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (self.PageIndex == 1) {
            [self.dataListArray removeAllObjects];
        }
        NSArray *arra = responseObject[@"result"];
        for (NSDictionary *dic in arra) {
            LKTestListModel *model = [LKTestListModel modelWithDictionary:dic];
            if ([model.isExamination boolValue] == YES) {
                [self.dataListArray addObject:model];
            }
        }
//        if (arra.count < self.dataListArray.count) {
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        if (self.dataListArray.count == 0) {
            self.tableView.mj_footer.hidden = YES;
        }
        [self.tableView reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
